package com.strawcom.charmring.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.strawcom.charmring.App;
import com.strawcom.charmring.C;
import com.strawcom.charmring.R;
import com.strawcom.charmring.bean.DailySummary;
import com.strawcom.charmring.ui.UserInfoActivity;
import com.strawcom.charmring.util.AnimUtil;
import com.strawcom.charmring.util.LogUtil;
import com.strawcom.charmring.util.PreferenceUtil;
import com.strawcom.charmring.view.MProgressView.OnProgressChangedListener;

/**
 * Exercise record weight
 * 
 * @author WangPeng
 */
public class ExerciseRerocdLayout extends FrameLayout implements OnProgressChangedListener {

	public static final int MAX_WEIGHT = 249;

	private MProgressView progressView;
	private TextView txValue;

	public ExerciseRerocdLayout(Context context) {
		this(context, null);
	}

	public ExerciseRerocdLayout(Context context, AttributeSet attrs) {
		super(context, attrs, 0);
		LayoutInflater.from(context).inflate(R.layout.lay_exercise_daily_record_weight, this);

		progressView = new MProgressView(context);
		progressView.setListener(this);
		((FrameLayout) findViewById(R.id.lay_progress)).addView(progressView);

		findViewById(R.id.lay_content_wrap).setOnClickListener(new OnClickListener() {
			@Override public void onClick(View v) {
				if(progressView.isEnable()) {
					final float value = Float.parseFloat(txValue.getText().toString());
					PreferenceUtil.write(getContext(), C.sp.exercise_record_weight, value);
					DailySummary dailySummary = App.i().getDailySummary();
					dailySummary.setWeight(value);
					dailySummary.update(dailySummary.getId());	
				}
				
				hide();
			}
		});

		findViewById(R.id.lay_content).setOnClickListener(new OnClickListener() {
			@Override public void onClick(View v) {
				// just intercepter click
			}
		});

		((TextView) findViewById(R.id.tx_title)).setTypeface(App.i().tfYouYuan);
		((TextView) findViewById(R.id.tx_tips)).setTypeface(App.i().tfYouYuan);
		((TextView) findViewById(R.id.tx_unit)).setTypeface(App.i().tfYouYuan);
		txValue = (TextView) findViewById(R.id.tx_value);
		txValue.setTypeface(App.i().tfYouYuan);

//		Button btnCancel = (Button) findViewById(R.id.btn_cancel);
//		btnCancel.setTypeface(App.i().tfYuppy);
//		btnCancel.setOnClickListener(new OnClickListener() {
//			@Override public void onClick(View v) {
//				hide();
//			}
//		});
//		Button btnConfirm = (Button) findViewById(R.id.btn_confirm);
//		btnConfirm.setTypeface(App.i().tfYuppy);
//		btnConfirm.setOnClickListener(new OnClickListener() {
//			@Override public void onClick(View v) {
//				String value = txValue.getText().toString();
//				PreferenceUtil.write(getContext(), C.sp.exercise_record_weight, Float.parseFloat(value));
//				hide();
//			}
//		});
	}

	/** 根据指定weight显示*/
	public void show(float weight, boolean editable) {
		clearAnimation();
		setVisibility(View.VISIBLE);
		startAnimation(AnimUtil.init().addAlpha(0f, 1f).addTranslate(0, 0, -getHeight(), 0, 0, 500, new DecelerateInterpolator()).create());
		
		if(editable) weight = PreferenceUtil.readFloat(getContext(), C.sp.exercise_record_weight);
		
		setRecordWeight(weight);
		progressView.setEnable(editable);
	}

	public void hide() {
		clearAnimation();
		startAnimation(AnimUtil.init().addAlpha(1f, 0f).addTranslate(0, 0, 0, -getHeight(), 0, 500, new DecelerateInterpolator()).create());
		setVisibility(View.INVISIBLE);
	}

	public void setRecordWeight(float weight) {
		LogUtil.d("ExerciseRerocdLayout setRecordWeight() weight:" + weight);
		progressView.setCurrentValue(weight);
	}

	public float getRecordWeight() {
		return progressView.getCurrentValue();
	}

	@Override public void onProgressChanged(float progress) {
		txValue.setText(String.format("%.1f", progress));
	}
}

